import hasRole from './hasRole'
import hasPermi from './hasPermi'
import {
  Message
} from 'element-ui'
const install = function(Vue) {
  Vue.directive('hasRole', hasRole)
  Vue.directive('hasPermi', hasPermi)
  // 自定义指令v-qianyu:
  Vue.directive('qianyu', {
    // inserted：被绑定元素插入父节点时调用
    inserted(el, binding, vnode) {
      const { value } = binding
      // console.log('自定义标签属性', el, binding, vnode, value)
    }
  })

  Vue.directive('filterTxt', {
    update: function(el, { value, modifiers }, vnode) {
      console.log('特殊符号',el, value, modifiers, vnode)
      // 限制字符长度80
      const defaultMaxlength = 80
      const elNode = el.querySelector('input') || el.querySelector('textarea')
      try {
        if (!vnode.data.model.value) {
          return false
        }
        // let pattern = new RegExp("[`~!#$%^&*()=|{}':;',\\[\\]<>/?￥]");
        const pattern = new RegExp(/[`~!#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/)
        // 预留之前 校验标准
        // let pattern = /[\%\<\>\&\"\'\/\\+\!\@\#\$\^\*\(\)\~\?\:\;\`\=\--\-\[\]]/g;
        if (pattern.test(elNode.value)) {
          elNode.value = vnode.data.model.value.replace(pattern, '')
          // Message.info('请勿输入特殊字符')
        } else {
          elNode.value = vnode.data.model.value.substring(0, defaultMaxlength)
        }
        if (el.querySelector('input')) {
          elNode.value = elNode.value.substring(0, defaultMaxlength)
        }
        elNode.dispatchEvent(new Event(modifiers.lazy ? 'change' : 'input'))
      } catch (e) {}
    }
  })
}

if (window.Vue) {
  window['hasRole'] = hasRole
  window['hasPermi'] = hasPermi
  Vue.use(install); // eslint-disable-line
}

export default install
